<script setup>
import {
    Edit,
    Delete
} from '@element-plus/icons-vue'
import { ref } from 'vue'
const resumes = ref([
    {
        "id": 1,
		"userId": 1,
		"resumeName": "123",
		"resumeType": "普通",
		"resumeFilePath": "123",
		"jobDescription": "1234",
		"internshipExperience": "213",
		"suggestions": "213",
		"createdAt": "2025-03-19 02:21:58",
		"updatedAt": "2025-03-19 02:21:58"
    },
    {
        "id": 1,
		"userId": 1,
		"resumeName": "123",
		"resumeType": "普通",
		"resumeFilePath": "123",
		"jobDescription": "1234",
		"internshipExperience": "213",
		"suggestions": "213",
		"createdAt": "2025-03-19 02:21:58",
		"updatedAt": "2025-03-19 02:21:58"
    },
    {
        "id": 1,
		"userId": 1,
		"resumeName": "123",
		"resumeType": "普通",
		"resumeFilePath": "123",
		"jobDescription": "1234",
		"internshipExperience": "213",
		"suggestions": "213",
		"createdAt": "2025-03-19 02:21:58",
		"updatedAt": "2025-03-19 02:21:58"
    }
])
const resume1 = ref([
    {
        "id": 1,
		"userId": 1,
		"resumeName": "123",
		"resumeType": "普通",
		"resumeFilePath": "123",
		"jobDescription": "1234",
		"internshipExperience": "213",
		"suggestions": "213",
		"createdAt": "2025-03-19 02:21:58",
		"updatedAt": "2025-03-19 02:21:58"
    },
    {
        "id": 1,
		"userId": 1,
		"resumeName": "123",
		"resumeType": "普通",
		"resumeFilePath": "123",
		"jobDescription": "1234",
		"internshipExperience": "213",
		"suggestions": "213",
		"createdAt": "2025-03-19 02:21:58",
		"updatedAt": "2025-03-19 02:21:58"
    },
    {
        "id": 1,
		"userId": 1,
		"resumeName": "123",
		"resumeType": "普通",
		"resumeFilePath": "123",
		"jobDescription": "1234",
		"internshipExperience": "213",
		"suggestions": "213",
		"createdAt": "2025-03-19 02:21:58",
		"updatedAt": "2025-03-19 02:21:58"
    }
])
//用户搜索时选中的分类名字
const resumeName=ref('')
const resumeType=ref('')
//分页条数据模型
const pageNum = ref(1)//当前页
const total = ref(20)//总条数
const pageSize = ref(3)//每页条数

//当每页条数发生了变化，调用此函数
const onSizeChange = (size) => {
    pageSize.value = size
    resumeList()
}
//当前页码发生变化，调用此函数
const onCurrentChange = (num) => {
    pageNum.value = num
    resumeList()
}

//声明一个异步的函数
import {resumeList1Service,resumeListService,resumeAddService,resumeUpdateService,resumeDeleteService} from '@/api/resume.js'
const resumeList = async()=>{
    let params ={
        pageNum:pageNum.value,
        pageSize:pageSize.value,
        resumeName:resumeName.value? resumeName.value:null,
        resumeType:resumeType.value? resumeType.value:null
    }
    let result = await resumeList1Service(params);
    let result1 = await resumeListService();
    //渲染视图
    total.value = result.data.total;
    resumes.value = result.data.items;
    resume1.value = result1.data;
}

resumeList();
const dialogVisible = ref(false)
//添加分类数据模型
const resumeModel = ref({
    resumeName: '',
    resumeType:''
})
//添加分类表单校验
const rules = {
    resumeName: [
        { required: true, message: '请输入简历名称', trigger: 'blur' },
    ],
    resumeType: [
        { required: true, message: '请输入简历类别', trigger: 'blur' },
    ]
}
//调用接口添加表单
import { ElMessage } from 'element-plus';
const addResume = async() =>{
    //调用接口
    let result = await resumeAddService(resumeModel.value);
    ElMessage.success(result.msg? result.msg:'添加成功')
    //调用列表函数
    resumeList();
    dialogVisible.value=false;
}
//定义变量
const title =ref()
//展示编辑弹窗
const showDialog =(row) =>{
    dialogVisible.value = true; title.value='编辑简历'
    //数据拷贝
    resumeModel.value.resumeName=row.resumeName;
    resumeModel.value.resumeType=row.resumeType;
    //扩展id属性
    resumeModel.value.id = row.id
}
//用户修改
const updateResume  = async() =>{
    //调用接口
    let result = await resumeUpdateService(resumeModel.value);
    ElMessage.success(result.msg? result.msg:'修改成功')
    //调用列表函数
    resumeList();
    dialogVisible.value=false;
}
//清空模型数据
const clearData=()=>{
    resumeModel.value.resumeName='';
    resumeModel.value.resumeType='';
}
//删除分类
import { ElMessageBox } from 'element-plus';
const deleteResume = (row)=>{
    ElMessageBox.confirm(
    '确认删除此事件分类？',
    '温馨提示',
    {
      confirmButtonText: '确认',
      cancelButtonText: '取消',
      type: 'warning',
    }
  )
    .then(async () => {
        let result = await resumeDeleteService(row.id)
      ElMessage({
        type: 'success',
        message: '删除成功',
      })
      //刷新列表
      resumeList();
    })
    .catch(() => {
      ElMessage({
        type: 'info',
        message: '取消删除',
      })
    })
}
</script>
<template>
    <el-card class="page-container">
        <template #header>
            <div class="header">
                <span><strong>简历类型</strong></span>
                <div class="extra">
                    <el-button type="primary" @click="dialogVisible=true;title='添加类型';clearData()">添加类型</el-button>
                </div>
            </div>
        </template>
                <!-- 搜索表单 -->
            <el-form inline>
            <el-form-item label="简历类型：">
                <el-select placeholder="请选择" v-model="resumeName" style="width: 240px">
                    <el-option 
                        v-for="c in resume1" 
                        :key="c.id" 
                        :label="c.resumeName"
                        :value="c.resumeName">
                    </el-option>
                </el-select>
            </el-form-item>

            <el-form-item>
                <el-button type="primary" @click="resumeList">搜索</el-button>
                <el-button @click="resumeName='';resumeList()">重置</el-button>
            </el-form-item>
        </el-form>

        <el-table :data="resumes" style="width: 100%">
            <el-table-column label="序号" width="100" type="index"> </el-table-column>
            <el-table-column label="用户ID" prop="userId"></el-table-column>
            <el-table-column label="简历名称" prop="resumeName"></el-table-column>
            <el-table-column label="简历类型" prop="resumeType"></el-table-column>
            <el-table-column label="存储路径" prop="resumeFilePath"></el-table-column>
            <el-table-column label="相关岗位描述" prop="jobDescription"></el-table-column>
            <el-table-column label="实习经历" prop="internshipExperience"></el-table-column>
            <el-table-column label="AI优化建议" prop="suggestions"></el-table-column>
            <el-table-column label="创建时间" prop="createdAt"></el-table-column>
            <el-table-column label="更新时间" prop="updatedAt"></el-table-column>
            <el-table-column label="操作" width="100">
                <template #default="{ row }">
                    <el-button :icon="Edit" circle plain type="primary" @click="showDialog(row)"></el-button>
                    <el-button :icon="Delete" circle plain type="danger" @click="deleteResume(row)"></el-button>
                </template>
            </el-table-column>
            <template #empty>
                <el-empty description="没有数据" />
            </template>
        </el-table>
        <!-- 分页条 -->
        <!-- 分页条 -->
        <el-pagination v-model:current-page="pageNum" v-model:page-size="pageSize" :page-sizes="[3, 5 ,10, 15]"
            layout="jumper, total, sizes, prev, pager, next" background :total="total" @size-change="onSizeChange"
            @current-change="onCurrentChange" style="margin-top: 20px; justify-content: flex-end" />
<!-- 添加分类弹窗 -->
        <el-dialog v-model="dialogVisible" :title="title" width="30%">
            <el-form :model="resumeModel" :rules="rules" label-width="100px" style="padding-right: 30px">
                <el-form-item label="简历名称" prop="resumeName">
                    <el-input v-model="resumeModel.resumeName" minlength="1" maxlength="10"></el-input>
                </el-form-item>
                <el-form-item label="简历类型" prop="resumeType">
                    <el-input v-model="resumeModel.resumeType" minlength="1" maxlength="15"></el-input>
                </el-form-item>
            </el-form>
            <template #footer>
                <span class="dialog-footer">
                    <el-button @click="dialogVisible = false">取消</el-button>
                    <el-button type="primary" @click="title=='添加分类'? addResume():updateResume()"> 确认 </el-button>
                </span>
            </template> 
        </el-dialog>

    </el-card>
</template>

<style lang="scss" scoped>
.page-container {
    min-height: 100%;
    box-sizing: border-box;

    .header {
        display: flex;
        align-items: center;
        justify-content: space-between;
    }
}
</style>